package huawei;

//  螺旋矩阵 II
public class Test59 {
    public static void main(String[] args) {
        int[][] ints = generateMatrix2(5);
        for (int[] anInt : ints) {
            soutArr(anInt);
        }
    }

    public static int[][] generateMatrix2(int n) {
        int [][] matrix = new int [n][n];
        int q = 0;
        int qCount = n / 2;
        int i = 0;
        int j = 0;
        int num = 1;
        while (q < qCount) {
            for (j = q; j < n - 1 - q; j++) {
                matrix[q][j] = num++;
            }
            for (i = q; i < n - 1 - q; i++) {
                matrix[i][j] = num++;
            }
            for (; j > q; j-- ) {
                matrix[i][j] = num++;
            }
            for (; i > q; i--) {
                matrix[i][j] = num++;
            }
            q++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2]= n*n;
        }
        return matrix;
    }


    public static void soutArr(int [] a){
        for (int i : a) {
            System.out.println(i);
        }
    }
}
